package com.thibal.lylo;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SearchActivity extends Activity implements OnClickListener{

	private Calendar mCalendar;

	private static final int DATE_DIALOG_ID = 0;
	private Spinner placeSpinner;
	private Spinner styleSpinner;
	private Button button;
	private Button datePicker;
	private TextView dateDisplay;
	private Context mContext;

	private int mYear = 0;
	private int mMonth = 0;
	private int mDay = 0;

	/** Called when the activity is first created. */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_layout);

		mContext = this;
		
		mYear = 0;
		mMonth = 0;
		mDay = 0;

		mCalendar = Calendar.getInstance();

		// Step 1: Locate our spinner control and save it to the class for convenience
		//         You could get it every time, I'm just being lazy...   :-)
		placeSpinner = (Spinner)this.findViewById(R.id.spinnerPlace);
		styleSpinner = (Spinner)this.findViewById(R.id.spinnerStyle);
		datePicker = (Button)this.findViewById(R.id.buttonDatePicker);
		button = (Button) this.findViewById(R.id.button1);
		dateDisplay = (TextView) this.findViewById(R.id.textView1);

		button.setOnClickListener(this);
		datePicker.setOnClickListener(this);

		// Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
		ArrayAdapter spinnerStyleArrayAdapter = new ArrayAdapter(this,
				R.layout.spinner_layout, R.id.text_spinner, new UrlsStyleParts[] {   
				new UrlsStyleParts( UrlsStyleParts.AllUrl, "All Style"),
				new UrlsStyleParts( UrlsStyleParts.ChansonUrl, "Chanson"), 
				new UrlsStyleParts( UrlsStyleParts.ElectroUrl, "Electro"),
				new UrlsStyleParts( UrlsStyleParts.GrooveUrl, "Groove"),
				new UrlsStyleParts( UrlsStyleParts.JazzUrl, "Jazz"),
				new UrlsStyleParts( UrlsStyleParts.MondeUrl, "Monde"),
				new UrlsStyleParts( UrlsStyleParts.ReggaeUrl, "Reggae"),
				new UrlsStyleParts( UrlsStyleParts.RockUrl, "Rock"),
		});

		ArrayAdapter spinnerPlaceArrayAdapter = new ArrayAdapter(this,
				R.layout.spinner_layout, R.id.text_spinner, new UrlsPlaceParts[] {   
				new UrlsPlaceParts( UrlsPlaceParts.AllPlacesUrl, "All places"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement1Url, "1er Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement2Url, "2e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement3Url, "3e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement4Url, "4e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement5Url, "5e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement6Url, "6e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement7Url, "7e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement8Url, "8e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement9Url, "9e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement10Url, "10e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement11Url, "11e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement12Url, "12e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement13Url, "13e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement14Url, "14e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement15Url, "15e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement16Url, "16e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement17Url, "17e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement18Url, "18e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement19Url, "19e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.Arrondissement20Url, "20e Arrondissement"),
				new UrlsPlaceParts( UrlsPlaceParts.EstUrl, "Est"),
				new UrlsPlaceParts( UrlsPlaceParts.NordUrl, "Nord"),
				new UrlsPlaceParts( UrlsPlaceParts.OuestUrl, "Ouest"),
				new UrlsPlaceParts( UrlsPlaceParts.SudUrl, "Sud"),
		});

		// Step 3: Tell the spinner about our adapter
		styleSpinner.setAdapter(spinnerStyleArrayAdapter);  
		placeSpinner.setAdapter(spinnerPlaceArrayAdapter);
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.equals(button))
		{
			Intent result = new Intent(this, LyloMainActivity.class);

			String styleUrl = ((UrlsStyleParts)(styleSpinner.getSelectedItem())).getUrl();
			String placeUrl = ((UrlsPlaceParts)(placeSpinner.getSelectedItem())).getUrl();
			String date = "";

			//ie if we chose a date
			if(mDay != 0)
			{
				if((mMonth+1) < 10)
					date = "-" + mYear + "-" + "0"+(mMonth+1);
				else
					date = "-" + mYear + "-" + (mMonth + 1);

				if(mDay < 10)
					date = date  + "-" + "0" + mDay;
				else
					date = date + "-" + mDay;
			}

			Bundle b = new Bundle();
			b.putStringArray("result", new String[]{styleUrl, date, placeUrl});

			result.putExtras(b);

			setResult(Activity.RESULT_OK, result);
			finish();
		}
		else
		{
			showDialog(DATE_DIALOG_ID);
		}
	}

	private void updateDisplay() {
		this.dateDisplay.setText(
				new StringBuilder()
				// Month is 0 based so add 1
				.append(mMonth + 1).append("-")
				.append(mDay).append("-")
				.append(mYear).append(" "));
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			mYear = mCalendar.get(Calendar.YEAR);
			mMonth = mCalendar.get(Calendar.MONTH);
			mDay = mCalendar.get(Calendar.DAY_OF_MONTH);
			return new DatePickerDialog(this,
					mDateSetListener,
					mYear, mMonth, mDay);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener =
		new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, 
				int monthOfYear, int dayOfMonth) {
			Calendar dayChosen = Calendar.getInstance();
			dayChosen.set(Calendar.DAY_OF_MONTH,dayOfMonth);
			dayChosen.set(Calendar.MONTH,monthOfYear); // 0-11 so 1 less
			dayChosen.set(Calendar.YEAR, year);
			long daysBetween = daysBetween(mCalendar, dayChosen);
			if(daysBetween < 8)
			{
				mYear = year;
				mMonth = monthOfYear;
				mDay = dayOfMonth;
				updateDisplay();
			}
			else
			{
				Toast.makeText(mContext, "Sorry, Lylo tells only the shows within a week :(", Toast.LENGTH_SHORT).show();
			}
		}
	};

	public static long daysBetween(Calendar startDate, Calendar endDate) {
		Calendar date = (Calendar) startDate.clone();
		long daysBetween = 0;
		while (date.before(endDate)) {
			date.add(Calendar.DAY_OF_MONTH, 1);
			daysBetween++;
		}
		return daysBetween;
	}
}
